//
//  RightViewController.m
//  SunXi
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "RightViewController.h"

@interface RightViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) RECview *baseView;

@end

@implementation RightViewController{
    NSArray *_Arr;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"已登录"];
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"自动登录"];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"退出登录" object:nil];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    if ([UserModel shareSingleton].userName > 0) {
         _Arr = @[NSLocalizedString(@"News", nil),NSLocalizedString(@"Friends", nil),NSLocalizedString(@"LogOff", nil)];
    }else{
        _Arr = @[@" ",@"请登录",@" "];
    }
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 54 * 2) / 2.0f, self.view.frame.size.width, 54 * _Arr.count) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.opaque = NO;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView = nil;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.bounces = NO;
        tableView;
    });
    [self.view addSubview:self.tableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(roleArr) name:@"已登录" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(roleArr) name:@"自动登录" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ExitDebarkation) name:@"退出登录" object:nil];
}
- (void)roleArr{
     _Arr = @[NSLocalizedString(@"News", nil),NSLocalizedString(@"Friends", nil),NSLocalizedString(@"LogOff", nil)];
    [self.tableView reloadData];
}
- (void)ExitDebarkation{
    _Arr = @[@" ",@"请登录",@" "];
    [self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![UserModel shareSingleton].userName.length > 0) {
        [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[UserLoadViewController alloc] init]] animated:YES];
        [self.sideMenuViewController hideMenuViewController];
        return;
    }
    
    switch (indexPath.row) {
        case 0:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[NewViewController alloc] init]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 1:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[FriendViewController alloc] init]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 2:
            [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
                if (!error) {
                    NSLog(@"退出成功");
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"退出成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
                    }];
                    [alertController addAction:cancelAction];
                    [self presentViewController:alertController animated:YES completion:nil];
                    [UserModel shareSingleton].userName = nil;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"退出登录" object:nil];
                }
            } onQueue:nil];
            break;
//        default:
//            break;
    }
}


#pragma mark UITableView Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return _Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"RightCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.7];
    }

    cell.textLabel.text = _Arr[indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentRight;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
